<template>
  <!-- 对话框表单 -->
  <el-dialog class="ba-operate-dialog" :close-on-click-modal="false" :model-value="baTable.form.operate ? true : false" @close="handleClose">
      <template #header>
          <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
              {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
          </div>
      </template>
      <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
          <el-tabs v-model="activeName" class="demo-tabs" :before-leave="handleTabClick">
              <el-tab-pane label="基本信息" name="a">
                  <basicForm v-if="baTable.form.operate" ref="basicFormRef" />
              </el-tab-pane>
              <el-tab-pane label="报价单成本明细" name="b">
                    <customerCost v-if="gstype == '1'&&activeName=='b'" :formOperate="baTable.form.operate"/>
                    <supplierCost v-if="gstype == '2'&&activeName=='b'" :formOperate="baTable.form.operate"/>
                    <!-- <linkman :formOperate="baTable.form.operate"/> -->
              </el-tab-pane>
              <el-tab-pane label="报价单收入明细" name="c" v-if="gstype == '1'">
                    <customerBody v-if="activeName=='c'" :formOperate="baTable.form.operate" />
                    <!-- <xCorpbank :formOperate="baTable.form.operate"/> -->
              </el-tab-pane>
          </el-tabs>
      </el-scrollbar>
      <template #footer v-if="activeName=='a'&&baTable.form.operate !=='view'">
          <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
              <el-button @click="baTable.toggleForm('')">{{ t('Cancel') }}</el-button>
              <el-button v-blur  v-if="!isApprove&&(baTable.form.items!.statuss=='未发起'||baTable.form.items!.statuss=='已退回'||!baTable.form.items!.statuss)" :loading="baTable.form.submitLoading" @click="handleSave" type="primary">
                  {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save') }}
              </el-button>
              <el-button v-if="isApprove&&baTable.form.items!.statuss=='待审批'" type="primary" @click="toApproval('approve')">审批</el-button>
          </div>
      </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref, inject, onMounted, computed ,provide,watch, watchEffect} from 'vue'
import { useI18n } from 'vue-i18n'
import type baTableClass from '/@/utils/baTable'
import FormItem from '/@/components/formItem/index.vue'
import { ElForm, ElMessage, FormItemRule } from 'element-plus'
import { buildValidatorData } from '/@/utils/validate'
import { codelib } from '/@/api/controllerUrls'
import createAxios from '/@/utils/axios'
import basicForm from './basicForm.vue'
import supplierCost from './supplier/bjCost/index.vue'
import customerCost from './customer/bjCost/index.vue'
import customerBody from './customer/bjBody/index.vue'
import { useRoute } from 'vue-router'
import { useApprovalInfo } from '/@/stores/approvalDialog'

// const formRef = ref<InstanceType<typeof ElForm>>()
const baTable = inject('baTable') as baTableClass

const { t } = useI18n()

const tabLock = ref(true)
const activeName = ref('a')
const basicFormRef = ref()
const gstype = inject('gstype') 
const bjno = ref<string|''>()
const transData:{
    from:string
    to:string
} = reactive({
    from:'',
    to:''
})

const route:any = useRoute()
const isApprove = computed(()=>{
    return route.name.indexOf('approve')>0
})

provide('bjno',bjno)
provide('isApprove',isApprove)
provide('transData',transData)

watchEffect(()=>{
    if((baTable.form.operate == 'edit'||baTable.form.operate == 'view')&&baTable.form.items!.BJNO){
        tabLock.value = false
        bjno.value = baTable.form.items!.BJNO
        transData.from = baTable.form.items!.TRANSLINE||''
        transData.to = baTable.form.items!.TRANSLINETO||''
    }
    if(baTable.form.operate == 'add'){
        tabLock.value = true
    }
})

const handleSave = ()=>{
  baTable.form.items!.gstype = gstype
  baTable.form.items!.type = 1
  basicFormRef.value.doSave((res:any)=>{
    tabLock.value = false
    if(res.data){
        bjno.value = res.data
        baTable.form.items!.BJNO = res.data
        baTable.form.operate = 'edit'
        baTable.form.items!.statuss = '未发起'
        transData.from = baTable.form.items!.TRANSLINE||''
        transData.to = baTable.form.items!.TRANSLINETO||''
    }
  })
}

const handleTabClick=()=>{
  if(tabLock.value){
      ElMessage({
          message:'请先保存表单',
          type:'warning'
      })
      return false
}else{
      return true
  }
}

const handleClose = ()=>{
  baTable.toggleForm()
  activeName.value = 'a'
}


//查看审批流
const approvalInfo = useApprovalInfo()

const toApproval = (type:string)=> {
    approvalInfo.setData({
        bizno:baTable.form.items!.BJNO,
        type,
        flowname:'',
    })
    approvalInfo.setDialogShow(true)
}

</script>

<style scoped lang="scss"></style>
